package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.IEnterpriseDao;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.entiy.base.ProgramPersonRela;
import com.magicode.cms.entiy.base.RepresentWorks;
import com.magicode.cms.entiy.base.Video;
import com.magicode.cms.service.IEnterpriseService;

@Service("serviceenterprise")
public class EnterpriseServiceImpl extends BaseServiceImpl<Enterprise, IEnterpriseDao> implements IEnterpriseService {

	@Autowired
	@Qualifier("daoenterprise")
	private IEnterpriseDao enterpriseDao;
	
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "机构名称");
		map.put("width", 200);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "initialWordDic");
		map.put(json_title, "首字母");
		map.put("width", 40);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "typesNames");
		map.put(json_title, "类型");
		map.put("width", 130);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "country");
		map.put(json_title, "国家");
		map.put("width", 140);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "status");
		map.put(json_title, "状态");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 40);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
//		Enterprise enterprise=null;
//		try {
//			enterprise = super.getObject(id);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		opt= new JsonOperation();
//		if(enterprise.getStatus()==0){
//			opt.setName("草稿");
//			opt.setType("preview");
//			opts.add(opt);
//		}else if(enterprise.getStatus()==1){
//			opt.setName("待审核");
//			opt.setType("preview");
//			opts.add(opt);
//		}else{
//			opt.setName("已发布");
//			opt.setType("preview");
//			opts.add(opt);
//		}
//		opt= new JsonOperation();
//		if(enterprise.getHead()==1){
//			opt.setName("已置顶");
//			opt.setType("preview");
//			opts.add(opt);
//		}else{
//			opt.setName("置顶");
//			opt.setUrl(getCtx().getContextPath()+"/"+url+"/zhiding/"+id);
//			opt.setType("preview");
//			opts.add(opt);
//		}
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/p/enterprise/sec/" + id);
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql =" from " +Enterprise.class.getName()+" "+entity_alias_name+" ";
		hql += " left join "+entity_alias_name+".initialWordDic as initialWordDic ";
		hql += " left join  "+entity_alias_name+".country as country ";
		hql += " left join "+entity_alias_name+".province as province ";
		hql += " left join "+entity_alias_name+".city as city ";
		hql += " left join "+entity_alias_name+".weichatMainPic as weichatMainPic ";
		hql += " left join "+entity_alias_name+".mainPic as mainPic ";

		if(null!=map && !map.isEmpty() && map.containsKey("typeId")){
			hql = hql+" , "+Dictionary.class.getName()+" tp ";
		}
		if(null!=map && !map.isEmpty()){
			hql += "where 1 = 1 ";
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get("title")+"%'";
			}
//			if(map.containsKey("typeId")){
//				hql = hql+" and "+entity_alias_name+".type.id = '"+map.get("typeId")+"'";
//			}
			if(map.containsKey("typeId")){
				hql = hql+" and tp.id = '"+map.get("typeId")+"' and tp in elements("+entity_alias_name+".types) ";
			}
			if(map.containsKey("initialWordDicId")){
				hql = hql+" and "+entity_alias_name+".initialWordDic.id = '"+map.get("initialWordDicId")+"'";
			}
			if(map.containsKey("countryId")){
				hql = hql+" and "+entity_alias_name+".country.id = '"+map.get("countryId")+"'";
			}
			if(map.containsKey("provinceId")){
				hql = hql+" and "+entity_alias_name+".province.id = '"+map.get("provinceId")+"'";
			}
			if(map.containsKey("cityId")){
				hql = hql+" and "+entity_alias_name+".city.id = '"+map.get("cityId")+"'";
			}
			if(map.containsKey("statusId")){
				hql = hql+" and "+entity_alias_name+".status = '"+map.get("statusId")+"' and "+entity_alias_name+".valid = 1 ";
			}
			if(map.containsKey("dateFrom")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".subcribeTime,'"+sql_date_format+"') >= '"+map.get("dateFrom")+"'";
			}
			if(map.containsKey("dateTo")){
				hql = hql+" and "+sql_date_method+"("+entity_alias_name+".subcribeTime,'"+sql_date_format+"') <= '"+map.get("dateTo")+"'";
			}
			
		}
//		hql += "order by lastUpdateTime desc ";
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += "order by t.createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("initialWordDic", "initialWordDic.title as initialWordDic ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("country", "country.title as country ");
			gridsel.put("status", "case when "+entity_alias_name+".status = 0 then '草稿' "
					+ " when "+entity_alias_name+".status = 1 then '待审核' "
					+ " when "+entity_alias_name+".status = 2 then '已发布'  end as status ");
//			gridsel.put("province", ""+entity_alias_name+".province.title as province ");
//			gridsel.put("type", ""+entity_alias_name+".type.title as type ");
			List<Map<String, Object>> resultList = this.queryMapDataList(hql, gridsel,start , size);
			if(null!=resultList &&resultList.size()>0){
				//关联机构
				String hql1 = "select distinct new map(t.id as id , d.title as title ) from "+Dictionary.class.getName()+" d , "+Enterprise.class.getName()+" t "
						+ " where  d in elements(t.types) " ;
				String propertyName = "typesNames";
				resultList =  this.getRelateProByList(hql1, ",", propertyName, resultList);
			}
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public IEnterpriseDao getDao() throws Exception {
		return enterpriseDao;
	}

	public IEnterpriseDao getEnterpriseDao() {
		return enterpriseDao;
	}

	public void setEnterpriseDao(IEnterpriseDao enterpriseDao) {
		this.enterpriseDao = enterpriseDao;
	}

	@Override
	public List<Video> getRelateVideos(String id) {
		List<Video> videoList = new ArrayList();
		Enterprise enterprise = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		String[] fetchJoins = new String[7];
		fetchJoins[0] = "mainPic";
		fetchJoins[1] = "province";
		fetchJoins[2] = "city";
		fetchJoins[3] = "initialWordDic";
//		fetchJoins[4] = "type";
		fetchJoins[4] = "weichatMainPic";
		fetchJoins[5] = "country";
		fetchJoins[6] = "relateVideos";
//		fetchJoins[7] = "relateVideos";
		try {
			enterprise =super.getObject(id,fetchJoins);
		} catch (Exception e) {
			e.printStackTrace();
		}
		videoList = enterprise.getRelateVideos();
		return videoList;
	}
	
	/**
	 * 查询作品年表列表
	 */
	@Override
	public List<RepresentWorks> getRepresentWorksList(String id) {
		List<RepresentWorks> list = new ArrayList<RepresentWorks>();
		String sql = " select p from "+RepresentWorks.class.getName()+" p , "+Enterprise.class.getName()+" t "
				+ " where p in elements(t.representWorksList) and t.id = :id order by p.id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			list = this.enterpriseDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Map<String,Object>> simpleSelect(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as text ");
			return this.queryMapDataList(hql, gridsel,0 , 50);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

}
